/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import com.oreilly.servlet.MultipartWrapper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.mapping.Formula;
import pojo.Book;
import pojo.BookHome;

/**
 *
 * @author amr
 */
public class UpdateBook extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println(request);
        
        System.out.println(" iam in do post updoad");
        String isbn = request.getParameter("book_isbn");
        String title = request.getParameter("book_title");
        String auther = request.getParameter("book_auther");
        String quantity = request.getParameter("book_quantity");
        String category = request.getParameter("book_category");
        String num_pages = request.getParameter("book_num_pages");
        String price = request.getParameter("book_price");

        System.out.println(request instanceof MultipartWrapper);
        if (request instanceof MultipartWrapper) {
            MultipartWrapper request1 = (MultipartWrapper) request;
            File f = request1.getFile("newCover");
            System.out.println(f.getName());
            System.out.println(" iam before fis");
            FileInputStream fis = new FileInputStream(f);
            int size = fis.available();
            byte[] b = new byte[size];
            fis.read(b);
            System.out.println(" iam after read");
            String name = f.getName();
            String[] split = name.split("\\.");
            File destPath = new File("images" + isbn + "." + split[1]);
            FileOutputStream fos = new FileOutputStream(destPath);
            fos.write(b);
            fis.close();
            fos.close();
            BookHome bookHome = new BookHome();
            Book book = new Book(Integer.parseInt(isbn), title, auther, ("images/" + isbn + "." + split[1]), category, Integer.parseInt(quantity), Double.parseDouble(price), Integer.parseInt(num_pages));
            bookHome.merge(book);
            System.out.println(" iam ended");
            String requestURI = request.getRequestURI();
            if (requestURI.contains("bookDetails.jsp")) {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/books.jsp");
                requestDispatcher.include(request, response);
            } else if (requestURI.contains("Add new book.jsp")) {
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Add new book.jsp");
                requestDispatcher.include(request, response);
            }

        }
    }
}
